//
//  YConfig.m
//  MUBATE
//
//  Created by sunbin on 2017/2/15.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "YConfig.h"
#import "UserDefaultsHeader.h"

@implementation YConfig

+ (void)saveOwnAccount:(NSString *)account andPassword:(NSString *)password
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:account ?: @"" forKey:kAccount];
    
    [userDefaults synchronize];
    [SSKeychain setPassword:password ?: @"" forService:kService account:account];
}


+ (NSArray *)getOwnAccountAndPassword
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *account = [userDefaults objectForKey:kAccount];
    NSString *password = [SSKeychain passwordForService:kService account:account] ?: @"";
    
    if (account) {return @[account, password];}
    return nil;
}




#pragma mark - user profile
+ (void)saveProfile:(YUserInfo *)user
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults setObject:user.store_name forKey:STORE_NAME];
 
    [userDefaults setObject:user.company_name forKey:COMPANY_NAME];
    [userDefaults setObject:user.store_name forKey:STORE_NAME];
    [userDefaults setObject:user.province forKey:PROVINCE];
    [userDefaults setObject:user.city forKey:CITY];
    [userDefaults setObject:user.area forKey:AREA];
    [userDefaults setObject:user.storeID forKey:STOREID];
    [userDefaults setObject:user.companyID forKey:COMPANYID];
    [userDefaults setObject:user.store_phone forKey:STORE_PHONE];
    [userDefaults setObject:user.store_email forKey:STORE_EMAIL];
    [userDefaults setObject:user.isValid forKey:ISVALID];
    [userDefaults setObject:user.shop_hours forKey:SHOP_HOURS];
    [userDefaults setObject:user.instruction forKey:INSTRUCTION];
    [userDefaults setObject:user.store_img forKey:STORE_IMG];
    [userDefaults setObject:user.address forKey:ADDRESS];
    [userDefaults setObject:user.dividend forKey:DIVIDEND];
    [userDefaults setObject:user.account_money forKey:ACCOUNT_MONEY];
    [userDefaults setObject:user.collect_money forKey:COLLECT_MONEY];
    [userDefaults setObject:user.freeze_money forKey:FREEZE_MONEY];
    [userDefaults setObject:user.trade_money forKey:TRADE_MONEY];
    [userDefaults setObject:user.trade_count forKey:TRADE_COUNT];
    [userDefaults setObject:user.dividend_money forKey:DIVIDEND_MONEY];
    
    [userDefaults setObject:user.company_code forKey:COMPANY_CODE];
    [userDefaults setObject:user.uid forKey:UID];
    [userDefaults setObject:user.company_real_name forKey:COMPANY_REAL_NAME];
    
    [userDefaults synchronize];

}


/**
 *  加载所有个人信息
 */

+ (YUserInfo *)myProfile
{
    YUserInfo *user = [YUserInfo new];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    
    user.store_name = [userDefaults objectForKey:STORE_NAME];
    user.company_name = [userDefaults objectForKey:COMPANY_NAME];
    user.province = [userDefaults objectForKey:PROVINCE];
    user.city = [userDefaults objectForKey:CITY];
    user.area = [userDefaults objectForKey:AREA];
    user.storeID = [userDefaults objectForKey:STOREID];
    user.companyID = [userDefaults objectForKey:COMPANYID];
    user.store_phone = [userDefaults objectForKey:STORE_PHONE];
    user.store_email = [userDefaults objectForKey:STORE_EMAIL];
    user.isValid = [userDefaults objectForKey:ISVALID];
    user.shop_hours = [userDefaults objectForKey:SHOP_HOURS];
    user.instruction = [userDefaults objectForKey:INSTRUCTION];
    user.store_img = [userDefaults objectForKey:STORE_IMG];
    user.address = [userDefaults objectForKey:ADDRESS];
    user.dividend = [userDefaults objectForKey:DIVIDEND];
    user.account_money = [userDefaults objectForKey:ACCOUNT_MONEY];
    user.collect_money = [userDefaults objectForKey:COLLECT_MONEY];
    user.freeze_money = [userDefaults objectForKey:FREEZE_MONEY];
    user.trade_money = [userDefaults objectForKey:TRADE_MONEY];
    user.trade_count = [userDefaults objectForKey:TRADE_COUNT];
    user.dividend_money = [userDefaults objectForKey:DIVIDEND_MONEY];

    
    user.company_code = [userDefaults objectForKey:COMPANY_CODE];
    user.uid = [userDefaults objectForKey:UID];
    user.company_real_name = [userDefaults objectForKey:COMPANY_REAL_NAME];
    

    return user;
}



#pragma make 个人uid
+ (void )saveOwnID:(NSString *)uid{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:uid forKey:UIDMAKE];
    [userDefaults synchronize];
}

+ (int64_t)getOwnID
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    return [userDefaults integerForKey:UIDMAKE];
}

#pragma make 清理操作
+ (void)clearProfile{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:kAccount];
    
    [userDefaults removeObjectForKey:STORE_NAME];
    [userDefaults removeObjectForKey:COMPANY_NAME];
    [userDefaults removeObjectForKey:PROVINCE];
    [userDefaults removeObjectForKey:CITY];
    [userDefaults removeObjectForKey:AREA];
    [userDefaults removeObjectForKey:STOREID];
    [userDefaults removeObjectForKey:COMPANYID];
    [userDefaults removeObjectForKey:STORE_PHONE];
    [userDefaults removeObjectForKey:STORE_EMAIL];
    [userDefaults removeObjectForKey:ISVALID];
    [userDefaults removeObjectForKey:SHOP_HOURS];
    [userDefaults removeObjectForKey:INSTRUCTION];
    [userDefaults removeObjectForKey:STORE_IMG];
    [userDefaults removeObjectForKey:ADDRESS];
    [userDefaults removeObjectForKey:DIVIDEND];
    [userDefaults removeObjectForKey:ACCOUNT_MONEY];
    [userDefaults removeObjectForKey:COLLECT_MONEY];
    [userDefaults removeObjectForKey:FREEZE_MONEY];
    [userDefaults removeObjectForKey:TRADE_MONEY];
    [userDefaults removeObjectForKey:TRADE_COUNT];
    [userDefaults removeObjectForKey:DIVIDEND_MONEY];
    
    [userDefaults removeObjectForKey:COMPANY_CODE];
    [userDefaults removeObjectForKey:UID];
    [userDefaults removeObjectForKey:COMPANY_REAL_NAME];
    [userDefaults removeObjectForKey:UIDMAKE];


    [userDefaults synchronize];
}


#pragma make 激光推送处理 激光RegistrationID

+ (void)saveRegistrationID:(NSString*)registID
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:registID forKey:REGISTIONID];
    [defaults synchronize];
}



+ (NSString *)registrationID
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:REGISTIONID];
}

+ (void)clearCookie
{
    // 删除缓存数据
    [ZL_NetworkCache removeAllHttpCache];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"sessionCookies"];
    [defaults removeObjectForKey:REGISTIONID];
}

@end
